<?php


namespace app\admin\order\controller;


use app\admin\order\logic\OrderLogic;
use app\admin\order\validate\OrderValidate;
use app\common\controller\BaseAdminController;
use app\model\order\Order;
use app\model\order\OrderStatusChange;
use plugin\exportcenter\app\service\ExportService;
use support\utils\Time;

/**
 * 订单管理
 * Class OrderController
 * @package app\admin\order\controller
 */
class OrderController extends BaseAdminController
{
    protected function init()
    {
        parent::init();
        $this->logic = new OrderLogic();
        $this->validate = new OrderValidate();
    }

    protected function searchWhere()
    {
        $where = [['is_del', '=', 0]];
        $this->form['order_no'] && $where[] = ['order_no', 'like', "%{$this->form['order_no']}%"];
        $this->form['mobile'] && $where[] = ['mobile', 'like', "%{$this->form['mobile']}%"];
        $this->form['user_id'] && $where[] = ['user_id', '=', $this->form['user_id']];
        $this->form['pay_way'] && $where[] = ['pay_way', '=', $this->form['pay_way']];
        $this->form['type'] && $where[] = ['type', '=', $this->form['type']];
        $this->form['pay_status'] && $where[] = ['pay_status', '=', $this->form['pay_status']];
        if ($this->form['create_start'] && $this->form['create_end']) {
            $where[] = ['create_time', 'between', [
                Time::getDayStartDateTime($this->form['create_start']),
                Time::getDayEndDateTime($this->form['create_end'])
            ]];
        }
        return $where;
    }

    public function list()
    {
        $where = [];
        $this->form['status'] && $where[] = ['status', '=', $this->form['status']];
        $query = $this->logic->search(array_merge($this->searchWhere(), $where));
        $query->order("create_time desc");
        $query->with([
            'user' => function ($query) {
                $query->field([
                    "id",
                    "username",
                    "nickname",
                    "avatar",
                    "mobile",
                ]);
            },
            'detail' => function ($query) {
                $query->field([
                    "id",
                    "order_id",
                    "goods_id",
                    "goods_name",
                    "price",
                    "logo",
                    "sku_id",
                    "sku_name",
                    "num",
                ]);
            },
        ]);
        $data = $this->logic->getPageList($query);

        $badge = Order::where($this->searchWhere())
            ->field([
                'status',
                'count(1) as count'
            ])
            ->group('status')
            ->select();
        $data['status_badge'] = array_column(collect($badge)->toArray(), 'count', 'status');
        $data['status_badge'][''] = array_sum(array_values($data['status_badge']));
        return $this->success($data);
    }

    /**
     * 订单发货
     */
    public function delivery()
    {
        $this->validate->checkData(input(), 'express');
        $this->logic->transaction(function () {
            $this->logic->orderDelivery(input());
        });
        return $this->success();
    }

    /**
     * 订单详情
     */
    public function read()
    {
        $this->validate->checkData(input(), 'id');
        $query = $this->logic->search(['id' => input('id')]);
        $query->with([
            'user' => function ($query) {
                $query->field([
                    "id",
                    "nickname",
                    "avatar",
                    "mobile",
                ]);
            },
            'detail',
            'address',
        ]);
        $data = $query->find();

        return $this->success($data);
    }


    public function getStatusMap()
    {
        return $this->success([
            "pay_status" => wrapperStatus(Order::PAY_STATUS_TEXT_MAP),
            "type" => wrapperStatus(Order::TYPE_TEXT_MAP),
            "status" => wrapperStatus(Order::STATUS_TEXT_MAP),
            "pay_way" => wrapperStatus(Order::PAY_WAY_TEXT_MAP),
        ]);
    }

    public function exportData()
    {
        $where = $this->searchWhere();
        $this->form['status'] && $where[] = ['status', '=', $this->form['status']];

        ExportService::export("订单列表" . Time::now(), "order", [
            'where' => $where,
        ]);
        return $this->success();
    }
}